/**
 * GExporter 1.0 - Exports your gmail address book with great flexibility.
 * Copyright (C) 2005 Karthik V (karthiksden@gmail.com)
 * This software is distributed under GNU GPL ( http://www.gnu.org/copyleft/gpl.html )
 */

package vkarthik.gtools.gexport;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.File;
import java.awt.event.*;
import java.awt.*;

public class GExView extends JFrame implements ActionListener {
static GExView theGExView;

JPanel pnPanel0;
JTabbedPane tbTabs;

JPanel pnPWhat;

JPanel pnPanel19;
JRadioButton rbAll;
JRadioButton rbSearch;
JTextField tfSearch;

JPanel pnPanel22;
JRadioButton rbCustom;
JComboBox cmbFields;
JComboBox cmbRule;
JTextField tfCustomSearch;
JButton btNextToHow;

JPanel pnPHow;

JPanel pnPanel8;

JPanel pnPanel14;
JRadioButton rbTab;
JRadioButton rbSpace;
JRadioButton rbOther;
JTextField tfOther;

JPanel pnPanel15;
JLabel lbLSeparation;
JTextField tfNumber;
JLabel lbLNumber;

JPanel pnPanel12;
JLabel lbLChooseFields;
JList lsOrigFields;
JList lsChosenFields;
JButton btAdd;
JButton btRemove;
JButton btBackToWhat;
JButton btNextToWhere;

JPanel pnPWhere;

JPanel pnPanel18;
JLabel lbLabel5;
JTextField tfFile;
JButton btBrowse;
JButton btNextToWho;
JButton btBackToHow;

JPanel pnPWho;
JButton btExport;
JButton btBackToWhere;

JPanel pnPanel39;

JPanel pnPanel35;
JLabel lbUser;
JLabel lbPass;

JPanel pnPanel37;
JTextField tfUser;
JLabel lbLabel20;
// JTextField tfPass;


// mod - start
JPasswordField tfPass;
DefaultListModel lmChosenFields;
DefaultListModel lmOrigFields;
ButtonGroup bgWhat;
ButtonGroup bgHow;
JFileChooser jfc = new JFileChooser();
GExInfo exData = new GExInfo();
boolean inputObtained = false;
// - end

/**
 */
public static void main(String args[]) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (ClassNotFoundException e) {
    }
    catch (InstantiationException e) {
    }
    catch (IllegalAccessException e) {
    }
    catch (UnsupportedLookAndFeelException e) {
    }
    theGExView = new GExView();
}

/**
 */
public GExView() {

	super("GExporter 1.0 by Karthik V - Export your gmail contacts the way you want!");
    pnPanel0 = new JPanel();
    GridBagLayout gbPanel0 = new GridBagLayout();
    GridBagConstraints gbcPanel0 = new GridBagConstraints();
    pnPanel0.setLayout(gbPanel0);

    tbTabs = new JTabbedPane();

    pnPWhat = new JPanel();
    pnPWhat.setToolTipText("What to export");
    GridBagLayout gbPWhat = new GridBagLayout();
    GridBagConstraints gbcPWhat = new GridBagConstraints();
    pnPWhat.setLayout(gbPWhat);

    pnPanel19 = new JPanel();
    GridBagLayout gbPanel19 = new GridBagLayout();
    GridBagConstraints gbcPanel19 = new GridBagConstraints();
    pnPanel19.setLayout(gbPanel19);

    rbAll = new JRadioButton("All contacts");
    rbAll.setToolTipText("Exports all available contacts");
    gbcPanel19.gridx = 2;
    gbcPanel19.gridy = 3;
    gbcPanel19.gridwidth = 4;
    gbcPanel19.gridheight = 2;
    gbcPanel19.fill = GridBagConstraints.BOTH;
    gbcPanel19.weightx = 1;
    gbcPanel19.weighty = 0;
    gbcPanel19.anchor = GridBagConstraints.NORTH;
    gbPanel19.setConstraints(rbAll, gbcPanel19);
    pnPanel19.add(rbAll);

    rbSearch = new JRadioButton("All contacts matching gmail search for ");
    rbSearch.setActionCommand("All Contacts");
    rbSearch.setSelected(true);
    rbSearch.setToolTipText("Searches gmail with the given string");
    gbcPanel19.gridx = 2;
    gbcPanel19.gridy = 6;
    gbcPanel19.gridwidth = 1;
    gbcPanel19.gridheight = 2;
    gbcPanel19.fill = GridBagConstraints.BOTH;
    gbcPanel19.weightx = 1;
    gbcPanel19.weighty = 0;
    gbcPanel19.anchor = GridBagConstraints.NORTH;
    gbcPanel19.insets = new Insets(20,0,20,0);
    gbPanel19.setConstraints(rbSearch, gbcPanel19);
    pnPanel19.add(rbSearch);

    tfSearch = new JTextField();
    tfSearch.setToolTipText("Enter a search string to query gmail");
    gbcPanel19.gridx = 3;
    gbcPanel19.gridy = 6;
    gbcPanel19.gridwidth = 10;
    gbcPanel19.gridheight = 2;
    gbcPanel19.fill = GridBagConstraints.HORIZONTAL;
    gbcPanel19.weightx = 1;
    gbcPanel19.weighty = 0;
    gbcPanel19.anchor = GridBagConstraints.NORTH;
    gbcPanel19.insets = new Insets(20,5,20,0);
    gbPanel19.setConstraints(tfSearch, gbcPanel19);
    pnPanel19.add(tfSearch);

    pnPanel22 = new JPanel();
    GridBagLayout gbPanel22 = new GridBagLayout();
    GridBagConstraints gbcPanel22 = new GridBagConstraints();
    pnPanel22.setLayout(gbPanel22);

    rbCustom = new JRadioButton("All contacts whose");
    rbCustom.setActionCommand("All Contacts");
    rbCustom.setToolTipText("Exports customized entries");
    gbcPanel22.gridx = 0;
    gbcPanel22.gridy = 1;
    gbcPanel22.gridwidth = 1;
    gbcPanel22.gridheight = 2;
    gbcPanel22.fill = GridBagConstraints.HORIZONTAL;
    gbcPanel22.weightx = 1;
    gbcPanel22.weighty = 0;
    gbcPanel22.anchor = GridBagConstraints.NORTH;
    gbPanel22.setConstraints(rbCustom, gbcPanel22);
    pnPanel22.add(rbCustom);

    String []lsCbFields = {"Name","ID","Email","Notes"}; // mod
    cmbFields = new JComboBox(lsCbFields);
    cmbFields.setMaximumRowCount(6);
    cmbFields.setToolTipText("Choose a field");
    gbcPanel22.gridx = 1;
    gbcPanel22.gridy = 1;
    gbcPanel22.gridwidth = 1;
    gbcPanel22.gridheight = 2;
    gbcPanel22.fill = GridBagConstraints.HORIZONTAL;
    gbcPanel22.weightx = 1;
    gbcPanel22.weighty = 0;
    gbcPanel22.anchor = GridBagConstraints.NORTH;
    gbcPanel22.insets = new Insets(0,5,0,5);
    gbPanel22.setConstraints(cmbFields, gbcPanel22);
    pnPanel22.add(cmbFields);

    String []lsCbRule = {"Equals","StartsWith","EndsWith","Contains"}; // mod
    cmbRule = new JComboBox(lsCbRule);
    cmbRule.setMaximumRowCount(5);
    cmbRule.setToolTipText("Choose a rule");
    gbcPanel22.gridx = 2;
    gbcPanel22.gridy = 1;
    gbcPanel22.gridwidth = 5;
    gbcPanel22.gridheight = 2;
    gbcPanel22.fill = GridBagConstraints.HORIZONTAL;
    gbcPanel22.weightx = 1;
    gbcPanel22.weighty = 0;
    gbcPanel22.anchor = GridBagConstraints.NORTH;
    gbcPanel22.insets = new Insets(0,5,0,5);
    gbPanel22.setConstraints(cmbRule, gbcPanel22);
    pnPanel22.add(cmbRule);

    tfCustomSearch = new JTextField();
    tfCustomSearch.setToolTipText("Enter a string to match");
    gbcPanel22.gridx = 7;
    gbcPanel22.gridy = 1;
    gbcPanel22.gridwidth = 5;
    gbcPanel22.gridheight = 2;
    gbcPanel22.fill = GridBagConstraints.HORIZONTAL;
    gbcPanel22.weightx = 1;
    gbcPanel22.weighty = 0;
    gbcPanel22.anchor = GridBagConstraints.NORTH;
    gbcPanel22.insets = new Insets(0,5,0,0);
    gbPanel22.setConstraints(tfCustomSearch, gbcPanel22);
    pnPanel22.add(tfCustomSearch);
    gbcPanel19.gridx = 2;
    gbcPanel19.gridy = 9;
    gbcPanel19.gridwidth = 14;
    gbcPanel19.gridheight = 4;
    gbcPanel19.fill = GridBagConstraints.BOTH;
    gbcPanel19.weightx = 1;
    gbcPanel19.weighty = 0;
    gbcPanel19.anchor = GridBagConstraints.NORTH;
    gbPanel19.setConstraints(pnPanel22, gbcPanel19);
    pnPanel19.add(pnPanel22);
    gbcPWhat.gridx = 1;
    gbcPWhat.gridy = 1;
    gbcPWhat.gridwidth = 21;
    gbcPWhat.gridheight = 6;
    gbcPWhat.fill = GridBagConstraints.BOTH;
    gbcPWhat.weightx = 1;
    gbcPWhat.weighty = 0;
    gbcPWhat.anchor = GridBagConstraints.NORTH;
    gbcPWhat.insets = new Insets(10,10,10,10);
    gbPWhat.setConstraints(pnPanel19, gbcPWhat);
    pnPWhat.add(pnPanel19);

    btNextToHow = new JButton("Next");
    gbcPWhat.gridx = 10;
    gbcPWhat.gridy = 10;
    gbcPWhat.gridwidth = 1;
    gbcPWhat.gridheight = 2;
    gbcPWhat.fill = GridBagConstraints.BOTH;
    gbcPWhat.weightx = 1;
    gbcPWhat.weighty = 0;
    gbcPWhat.anchor = GridBagConstraints.NORTH;
    gbcPWhat.insets = new Insets(30,50,0,50);
    gbPWhat.setConstraints(btNextToHow, gbcPWhat);
    pnPWhat.add(btNextToHow);
    tbTabs.addTab("What to Export",pnPWhat);

    pnPHow = new JPanel();
    GridBagLayout gbPHow = new GridBagLayout();
    GridBagConstraints gbcPHow = new GridBagConstraints();
    pnPHow.setLayout(gbPHow);

    pnPanel8 = new JPanel();
    GridBagLayout gbPanel8 = new GridBagLayout();
    GridBagConstraints gbcPanel8 = new GridBagConstraints();
    pnPanel8.setLayout(gbPanel8);

    pnPanel14 = new JPanel();
    GridBagLayout gbPanel14 = new GridBagLayout();
    GridBagConstraints gbcPanel14 = new GridBagConstraints();
    pnPanel14.setLayout(gbPanel14);

    rbTab = new JRadioButton("Tabs");
    rbTab.setSelected(true);
    gbcPanel14.gridx = 0;
    gbcPanel14.gridy = 1;
    gbcPanel14.gridwidth = 4;
    gbcPanel14.gridheight = 1;
    gbcPanel14.fill = GridBagConstraints.BOTH;
    gbcPanel14.weightx = 1;
    gbcPanel14.weighty = 0;
    gbcPanel14.anchor = GridBagConstraints.NORTH;
    gbPanel14.setConstraints(rbTab, gbcPanel14);
    pnPanel14.add(rbTab);

    rbSpace = new JRadioButton("Spaces");
    rbSpace.setActionCommand("Tabs");
    rbSpace.setSelected(true);
    gbcPanel14.gridx = 5;
    gbcPanel14.gridy = 1;
    gbcPanel14.gridwidth = 4;
    gbcPanel14.gridheight = 1;
    gbcPanel14.fill = GridBagConstraints.BOTH;
    gbcPanel14.weightx = 1;
    gbcPanel14.weighty = 0;
    gbcPanel14.anchor = GridBagConstraints.NORTH;
    gbPanel14.setConstraints(rbSpace, gbcPanel14);
    pnPanel14.add(rbSpace);

    rbOther = new JRadioButton("Other");
    rbOther.setActionCommand("Tabs");
    gbcPanel14.gridx = 10;
    gbcPanel14.gridy = 1;
    gbcPanel14.gridwidth = 4;
    gbcPanel14.gridheight = 1;
    gbcPanel14.fill = GridBagConstraints.BOTH;
    gbcPanel14.weightx = 1;
    gbcPanel14.weighty = 0;
    gbcPanel14.anchor = GridBagConstraints.NORTH;
    gbPanel14.setConstraints(rbOther, gbcPanel14);
    pnPanel14.add(rbOther);

    tfOther = new JTextField();
    gbcPanel14.gridx = 14;
    gbcPanel14.gridy = 1;
    gbcPanel14.gridwidth = 2;
    gbcPanel14.gridheight = 1;
    gbcPanel14.fill = GridBagConstraints.HORIZONTAL;
    gbcPanel14.weightx = 1;
    gbcPanel14.weighty = 0;
    gbcPanel14.anchor = GridBagConstraints.NORTH;
    gbPanel14.setConstraints(tfOther, gbcPanel14);
    pnPanel14.add(tfOther);
    gbcPanel8.gridx = 1;
    gbcPanel8.gridy = 4;
    gbcPanel8.gridwidth = 19;
    gbcPanel8.gridheight = 2;
    gbcPanel8.fill = GridBagConstraints.BOTH;
    gbcPanel8.weightx = 1;
    gbcPanel8.weighty = 0;
    gbcPanel8.anchor = GridBagConstraints.NORTH;
    gbcPanel8.insets = new Insets(5,0,0,0);
    gbPanel8.setConstraints(pnPanel14, gbcPanel8);
    pnPanel8.add(pnPanel14);

    pnPanel15 = new JPanel();
    GridBagLayout gbPanel15 = new GridBagLayout();
    GridBagConstraints gbcPanel15 = new GridBagConstraints();
    pnPanel15.setLayout(gbPanel15);

    lbLSeparation = new JLabel("Separate the fields with ");
    gbcPanel15.gridx = 1;
    gbcPanel15.gridy = 1;
    gbcPanel15.gridwidth = 4;
    gbcPanel15.gridheight = 1;
    gbcPanel15.fill = GridBagConstraints.BOTH;
    gbcPanel15.weightx = 1;
    gbcPanel15.weighty = 1;
    gbcPanel15.anchor = GridBagConstraints.NORTH;
    gbPanel15.setConstraints(lbLSeparation, gbcPanel15);
    pnPanel15.add(lbLSeparation);

    tfNumber = new JTextField();
    gbcPanel15.gridx = 5;
    gbcPanel15.gridy = 1;
    gbcPanel15.gridwidth = 3;
    gbcPanel15.gridheight = 1;
    gbcPanel15.fill = GridBagConstraints.HORIZONTAL;
    gbcPanel15.weightx = 1;
    gbcPanel15.weighty = 0;
    gbcPanel15.anchor = GridBagConstraints.NORTH;
    gbcPanel15.insets = new Insets(0,5,0,5);
    gbPanel15.setConstraints(tfNumber, gbcPanel15);
    pnPanel15.add(tfNumber);

    lbLNumber = new JLabel("number(s) of");
    gbcPanel15.gridx = 8;
    gbcPanel15.gridy = 1;
    gbcPanel15.gridwidth = 5;
    gbcPanel15.gridheight = 1;
    gbcPanel15.fill = GridBagConstraints.BOTH;
    gbcPanel15.weightx = 1;
    gbcPanel15.weighty = 1;
    gbcPanel15.anchor = GridBagConstraints.NORTH;
    gbPanel15.setConstraints(lbLNumber, gbcPanel15);
    pnPanel15.add(lbLNumber);
    gbcPanel8.gridx = 1;
    gbcPanel8.gridy = 1;
    gbcPanel8.gridwidth = 19;
    gbcPanel8.gridheight = 2;
    gbcPanel8.fill = GridBagConstraints.BOTH;
    gbcPanel8.weightx = 1;
    gbcPanel8.weighty = 0;
    gbcPanel8.anchor = GridBagConstraints.NORTH;
    gbPanel8.setConstraints(pnPanel15, gbcPanel8);
    pnPanel8.add(pnPanel15);
    gbcPHow.gridx = 1;
    gbcPHow.gridy = 16;
    gbcPHow.gridwidth = 15;
    gbcPHow.gridheight = 5;
    gbcPHow.fill = GridBagConstraints.BOTH;
    gbcPHow.weightx = 1;
    gbcPHow.weighty = 0;
    gbcPHow.anchor = GridBagConstraints.SOUTH;
    gbcPHow.insets = new Insets(10,10,10,10);
    gbPHow.setConstraints(pnPanel8, gbcPHow);
    pnPHow.add(pnPanel8);

    pnPanel12 = new JPanel();
    GridBagLayout gbPanel12 = new GridBagLayout();
    GridBagConstraints gbcPanel12 = new GridBagConstraints();
    pnPanel12.setLayout(gbPanel12);

    lbLChooseFields = new JLabel("Choose the fields to export");
    gbcPanel12.gridx = 0;
    gbcPanel12.gridy = 0;
    gbcPanel12.gridwidth = 10;
    gbcPanel12.gridheight = 1;
    gbcPanel12.fill = GridBagConstraints.BOTH;
    gbcPanel12.weightx = 1;
    gbcPanel12.weighty = 1;
    gbcPanel12.anchor = GridBagConstraints.NORTH;
    gbcPanel12.insets = new Insets(0,0,10,0);
    gbPanel12.setConstraints(lbLChooseFields, gbcPanel12);
    pnPanel12.add(lbLChooseFields);

  	// mod - start
  	lmOrigFields = new DefaultListModel();
	lmOrigFields.addElement("Name");
	lmOrigFields.addElement("ID (used by gmail)");
	lmOrigFields.addElement("Email");
	lmOrigFields.addElement("Notes");
    lsOrigFields = new JList(lmOrigFields);
    // - end
    lsOrigFields.setToolTipText("Available fields in gmail address book");
    JScrollPane scpOrigFields = new JScrollPane(lsOrigFields);
    gbcPanel12.gridx = 0;
    gbcPanel12.gridy = 2;
    gbcPanel12.gridwidth = 7;
    gbcPanel12.gridheight = 5;
    gbcPanel12.fill = GridBagConstraints.BOTH;
    gbcPanel12.weightx = 1;
    gbcPanel12.weighty = 1;
    gbcPanel12.anchor = GridBagConstraints.NORTH;
    gbPanel12.setConstraints(scpOrigFields, gbcPanel12);
    pnPanel12.add(scpOrigFields);
	// mod - start
    lmChosenFields = new DefaultListModel();
    lsChosenFields = new JList(lmChosenFields);
    // - end
    lsChosenFields.setToolTipText("List of fields to be exported");
    JScrollPane scpChosenFields = new JScrollPane(lsChosenFields);
    gbcPanel12.gridx = 11;
    gbcPanel12.gridy = 2;
    gbcPanel12.gridwidth = 7;
    gbcPanel12.gridheight = 5;
    gbcPanel12.fill = GridBagConstraints.BOTH;
    gbcPanel12.weightx = 1;
    gbcPanel12.weighty = 1;
    gbcPanel12.anchor = GridBagConstraints.NORTH;
    gbPanel12.setConstraints(scpChosenFields, gbcPanel12);
    pnPanel12.add(scpChosenFields);

    btAdd = new JButton("Add");
    gbcPanel12.gridx = 8;
    gbcPanel12.gridy = 3;
    gbcPanel12.gridwidth = 2;
    gbcPanel12.gridheight = 1;
    gbcPanel12.fill = GridBagConstraints.BOTH;
    gbcPanel12.weightx = 1;
    gbcPanel12.weighty = 0;
    gbcPanel12.anchor = GridBagConstraints.NORTH;
    gbcPanel12.insets = new Insets(25,10,10,10);
    gbPanel12.setConstraints(btAdd, gbcPanel12);
    pnPanel12.add(btAdd);

    btRemove = new JButton("Remove");
    btRemove.setActionCommand("Add");
    gbcPanel12.gridx = 8;
    gbcPanel12.gridy = 5;
    gbcPanel12.gridwidth = 2;
    gbcPanel12.gridheight = 1;
    gbcPanel12.fill = GridBagConstraints.BOTH;
    gbcPanel12.weightx = 1;
    gbcPanel12.weighty = 0;
    gbcPanel12.anchor = GridBagConstraints.NORTH;
    gbcPanel12.insets = new Insets(0,10,0,10);
    gbPanel12.setConstraints(btRemove, gbcPanel12);
    pnPanel12.add(btRemove);
    gbcPHow.gridx = 1;
    gbcPHow.gridy = 1;
    gbcPHow.gridwidth = 15;
    gbcPHow.gridheight = 14;
    gbcPHow.fill = GridBagConstraints.BOTH;
    gbcPHow.weightx = 1;
    gbcPHow.weighty = 0;
    gbcPHow.anchor = GridBagConstraints.NORTH;
    gbcPHow.insets = new Insets(10,10,10,10);
    gbPHow.setConstraints(pnPanel12, gbcPHow);
    pnPHow.add(pnPanel12);

    btBackToWhat = new JButton("Back");
    gbcPHow.gridx = 5;
    gbcPHow.gridy = 22;
    gbcPHow.gridwidth = 3;
    gbcPHow.gridheight = 2;
    gbcPHow.fill = GridBagConstraints.BOTH;
    gbcPHow.weightx = 1;
    gbcPHow.weighty = 0;
    gbcPHow.anchor = GridBagConstraints.SOUTH;
    gbcPHow.insets = new Insets(30,50,0,0);
    gbPHow.setConstraints(btBackToWhat, gbcPHow);
    pnPHow.add(btBackToWhat);

    btNextToWhere = new JButton("Next");
    gbcPHow.gridx = 8;
    gbcPHow.gridy = 22;
    gbcPHow.gridwidth = 3;
    gbcPHow.gridheight = 2;
    gbcPHow.fill = GridBagConstraints.BOTH;
    gbcPHow.weightx = 1;
    gbcPHow.weighty = 0;
    gbcPHow.anchor = GridBagConstraints.SOUTH;
    gbcPHow.insets = new Insets(30,20,0,50);
    gbPHow.setConstraints(btNextToWhere, gbcPHow);
    pnPHow.add(btNextToWhere);
    tbTabs.addTab("How to Export",pnPHow);

    pnPWhere = new JPanel();
    GridBagLayout gbPWhere = new GridBagLayout();
    GridBagConstraints gbcPWhere = new GridBagConstraints();
    pnPWhere.setLayout(gbPWhere);

    pnPanel18 = new JPanel();
    GridBagLayout gbPanel18 = new GridBagLayout();
    GridBagConstraints gbcPanel18 = new GridBagConstraints();
    pnPanel18.setLayout(gbPanel18);

    lbLabel5 = new JLabel("Choose the file");
    gbcPanel18.gridx = 1;
    gbcPanel18.gridy = 1;
    gbcPanel18.gridwidth = 2;
    gbcPanel18.gridheight = 2;
    gbcPanel18.fill = GridBagConstraints.BOTH;
    gbcPanel18.weightx = 1;
    gbcPanel18.weighty = 1;
    gbcPanel18.anchor = GridBagConstraints.NORTH;
    gbcPanel18.insets = new Insets(20,0,0,0);
    gbPanel18.setConstraints(lbLabel5, gbcPanel18);
    pnPanel18.add(lbLabel5);

    tfFile = new JTextField();
    gbcPanel18.gridx = 3;
    gbcPanel18.gridy = 1;
    gbcPanel18.gridwidth = 12;
    gbcPanel18.gridheight = 2;
    gbcPanel18.fill = GridBagConstraints.HORIZONTAL;
    gbcPanel18.weightx = 1;
    gbcPanel18.weighty = 0;
    gbcPanel18.anchor = GridBagConstraints.NORTH;
    gbcPanel18.insets = new Insets(20,10,0,0);
    gbPanel18.setConstraints(tfFile, gbcPanel18);
    pnPanel18.add(tfFile);

    btBrowse = new JButton("Browse");
    gbcPanel18.gridx = 15;
    gbcPanel18.gridy = 1;
    gbcPanel18.gridwidth = 4;
    gbcPanel18.gridheight = 1;
    gbcPanel18.fill = GridBagConstraints.BOTH;
    gbcPanel18.weightx = 1;
    gbcPanel18.weighty = 0;
    gbcPanel18.anchor = GridBagConstraints.NORTH;
    gbcPanel18.insets = new Insets(20,5,0,0);
    gbPanel18.setConstraints(btBrowse, gbcPanel18);
    pnPanel18.add(btBrowse);
    gbcPWhere.gridx = 2;
    gbcPWhere.gridy = 2;
    gbcPWhere.gridwidth = 16;
    gbcPWhere.gridheight = 1;
    gbcPWhere.fill = GridBagConstraints.BOTH;
    gbcPWhere.weightx = 1;
    gbcPWhere.weighty = 0;
    gbcPWhere.anchor = GridBagConstraints.NORTH;
    gbcPWhere.insets = new Insets(10,10,10,10);
    gbPWhere.setConstraints(pnPanel18, gbcPWhere);
    pnPWhere.add(pnPanel18);

    btNextToWho = new JButton("Next");
    gbcPWhere.gridx = 10;
    gbcPWhere.gridy = 4;
    gbcPWhere.gridwidth = 4;
    gbcPWhere.gridheight = 2;
    gbcPWhere.fill = GridBagConstraints.BOTH;
    gbcPWhere.weightx = 1;
    gbcPWhere.weighty = 0;
    gbcPWhere.anchor = GridBagConstraints.NORTH;
    gbcPWhere.insets = new Insets(30,20,0,50);
    gbPWhere.setConstraints(btNextToWho, gbcPWhere);
    pnPWhere.add(btNextToWho);

    btBackToHow = new JButton("Back");
    btBackToHow.setActionCommand("Export");
    gbcPWhere.gridx = 6;
    gbcPWhere.gridy = 4;
    gbcPWhere.gridwidth = 4;
    gbcPWhere.gridheight = 2;
    gbcPWhere.fill = GridBagConstraints.BOTH;
    gbcPWhere.weightx = 1;
    gbcPWhere.weighty = 0;
    gbcPWhere.anchor = GridBagConstraints.NORTH;
    gbcPWhere.insets = new Insets(30,50,0,0);
    gbPWhere.setConstraints(btBackToHow, gbcPWhere);
    pnPWhere.add(btBackToHow);
    tbTabs.addTab("Where to Export",pnPWhere);

    pnPWho = new JPanel();
    GridBagLayout gbPWho = new GridBagLayout();
    GridBagConstraints gbcPWho = new GridBagConstraints();
    pnPWho.setLayout(gbPWho);

    btExport = new JButton("Export");
    gbcPWho.gridx = 13;
    gbcPWho.gridy = 9;
    gbcPWho.gridwidth = 4;
    gbcPWho.gridheight = 2;
    gbcPWho.fill = GridBagConstraints.BOTH;
    gbcPWho.weightx = 1;
    gbcPWho.weighty = 0;
    gbcPWho.anchor = GridBagConstraints.NORTH;
    gbcPWho.insets = new Insets(30,20,0,50);
    gbPWho.setConstraints(btExport, gbcPWho);
    pnPWho.add(btExport);

    btBackToWhere = new JButton("Back");
    btBackToWhere.setActionCommand("Export");
    gbcPWho.gridx = 9;
    gbcPWho.gridy = 9;
    gbcPWho.gridwidth = 4;
    gbcPWho.gridheight = 2;
    gbcPWho.fill = GridBagConstraints.BOTH;
    gbcPWho.weightx = 1;
    gbcPWho.weighty = 0;
    gbcPWho.anchor = GridBagConstraints.NORTH;
    gbcPWho.insets = new Insets(30,50,0,0);
    gbPWho.setConstraints(btBackToWhere, gbcPWho);
    pnPWho.add(btBackToWhere);

    pnPanel39 = new JPanel();
    GridBagLayout gbPanel39 = new GridBagLayout();
    GridBagConstraints gbcPanel39 = new GridBagConstraints();
    pnPanel39.setLayout(gbPanel39);

    pnPanel35 = new JPanel();
    GridBagLayout gbPanel35 = new GridBagLayout();
    GridBagConstraints gbcPanel35 = new GridBagConstraints();
    pnPanel35.setLayout(gbPanel35);

    lbUser = new JLabel("Gmail Id");
    gbcPanel35.gridx = 0;
    gbcPanel35.gridy = 0;
    gbcPanel35.gridwidth = 3;
    gbcPanel35.gridheight = 1;
    gbcPanel35.fill = GridBagConstraints.BOTH;
    gbcPanel35.weightx = 1;
    gbcPanel35.weighty = 1;
    gbcPanel35.anchor = GridBagConstraints.NORTH;
    gbPanel35.setConstraints(lbUser, gbcPanel35);
    pnPanel35.add(lbUser);

    lbPass = new JLabel("Password");
    gbcPanel35.gridx = 0;
    gbcPanel35.gridy = 2;
    gbcPanel35.gridwidth = 3;
    gbcPanel35.gridheight = 1;
    gbcPanel35.fill = GridBagConstraints.BOTH;
    gbcPanel35.weightx = 1;
    gbcPanel35.weighty = 1;
    gbcPanel35.anchor = GridBagConstraints.NORTH;
    gbcPanel35.insets = new Insets(10,0,0,0);
    gbPanel35.setConstraints(lbPass, gbcPanel35);
    pnPanel35.add(lbPass);
    gbcPanel39.gridx = 4;
    gbcPanel39.gridy = 1;
    gbcPanel39.gridwidth = 3;
    gbcPanel39.gridheight = 3;
    gbcPanel39.fill = GridBagConstraints.BOTH;
    gbcPanel39.weightx = 1;
    gbcPanel39.weighty = 0;
    gbcPanel39.anchor = GridBagConstraints.NORTH;
    gbPanel39.setConstraints(pnPanel35, gbcPanel39);
    pnPanel39.add(pnPanel35);

    pnPanel37 = new JPanel();
    GridBagLayout gbPanel37 = new GridBagLayout();
    GridBagConstraints gbcPanel37 = new GridBagConstraints();
    pnPanel37.setLayout(gbPanel37);

    tfUser = new JTextField();
    gbcPanel37.gridx = 0;
    gbcPanel37.gridy = 0;
    gbcPanel37.gridwidth = 5;
    gbcPanel37.gridheight = 1;
    gbcPanel37.fill = GridBagConstraints.BOTH;
    gbcPanel37.weightx = 1;
    gbcPanel37.weighty = 0;
    gbcPanel37.anchor = GridBagConstraints.NORTH;
    gbPanel37.setConstraints(tfUser, gbcPanel37);
    pnPanel37.add(tfUser);

    lbLabel20 = new JLabel("@gmail.com");
    gbcPanel37.gridx = 5;
    gbcPanel37.gridy = 0;
    gbcPanel37.gridwidth = 3;
    gbcPanel37.gridheight = 1;
    gbcPanel37.fill = GridBagConstraints.BOTH;
    gbcPanel37.weightx = 1;
    gbcPanel37.weighty = 1;
    gbcPanel37.anchor = GridBagConstraints.NORTH;
    gbPanel37.setConstraints(lbLabel20, gbcPanel37);
    pnPanel37.add(lbLabel20);

    tfPass = new JPasswordField(); // mod
    gbcPanel37.gridx = 0;
    gbcPanel37.gridy = 2;
    gbcPanel37.gridwidth = 5;
    gbcPanel37.gridheight = 1;
    gbcPanel37.fill = GridBagConstraints.BOTH;
    gbcPanel37.weightx = 1;
    gbcPanel37.weighty = 0;
    gbcPanel37.anchor = GridBagConstraints.NORTH;
    gbcPanel37.insets = new Insets(10,0,0,0);
    gbPanel37.setConstraints(tfPass, gbcPanel37);
    pnPanel37.add(tfPass);
    gbcPanel39.gridx = 7;
    gbcPanel39.gridy = 1;
    gbcPanel39.gridwidth = 8;
    gbcPanel39.gridheight = 3;
    gbcPanel39.fill = GridBagConstraints.BOTH;
    gbcPanel39.weightx = 1;
    gbcPanel39.weighty = 0;
    gbcPanel39.anchor = GridBagConstraints.NORTH;
    gbcPanel39.insets = new Insets(0,10,0,10);
    gbPanel39.setConstraints(pnPanel37, gbcPanel39);
    pnPanel39.add(pnPanel37);
    gbcPWho.gridx = 6;
    gbcPWho.gridy = 3;
    gbcPWho.gridwidth = 14;
    gbcPWho.gridheight = 5;
    gbcPWho.fill = GridBagConstraints.BOTH;
    gbcPWho.weightx = 1;
    gbcPWho.weighty = 0;
    gbcPWho.anchor = GridBagConstraints.NORTH;
    gbPWho.setConstraints(pnPanel39, gbcPWho);
    pnPWho.add(pnPanel39);
    tbTabs.addTab("User Details",pnPWho);
    gbcPanel0.gridx = 0;
    gbcPanel0.gridy = 0;
    gbcPanel0.gridwidth = 1;
    gbcPanel0.gridheight = 1;
    gbcPanel0.fill = GridBagConstraints.BOTH;
    gbcPanel0.weightx = 1;
    gbcPanel0.weighty = 1;
    gbcPanel0.anchor = GridBagConstraints.NORTH;
    gbPanel0.setConstraints(tbTabs, gbcPanel0);
    pnPanel0.add(tbTabs);



    bgWhat = new ButtonGroup();
    bgWhat.add(rbAll);
    bgWhat.add(rbSearch);
    bgWhat.add(rbCustom);
    bgHow = new ButtonGroup();
    bgHow.add(rbTab);
    bgHow.add(rbSpace);
    bgHow.add(rbOther);
    rbAll.setSelected(true);
    rbTab.setSelected(true);
    tfNumber.setText("1");
    tfFile.setText("contacts.dat");
    btNextToHow.addActionListener(this);
    btNextToWhere.addActionListener(this);
    btNextToWho.addActionListener(this);
    btBackToWhat.addActionListener(this);
    btBackToHow.addActionListener(this);
    btBackToWhere.addActionListener(this);
    btExport.addActionListener(this);
    btAdd.addActionListener(this);
    btRemove.addActionListener(this);
    btBrowse.addActionListener(this);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setContentPane(pnPanel0);
    pack();
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Dimension dim   = toolkit.getScreenSize();
	int width = 500, height = 400;
	setLocation((int)((dim.getWidth() - width) / 2), (int)((dim.getHeight() - height) / 2));
 	setSize(width,height);
    show();
}

public void actionPerformed(ActionEvent e) {

	String temp = null;
	if(e.getSource() == btNextToHow) {
		if((temp = validateWhatTab()) != null) {
			JOptionPane.showMessageDialog(this, temp, "Error", JOptionPane.ERROR_MESSAGE);
			tbTabs.setSelectedIndex(0);
			return;
		}
		tbTabs.setSelectedIndex(1);
	}
	else if(e.getSource() == btNextToWhere) {
		if((temp = validateHowTab()) != null) {
			JOptionPane.showMessageDialog(this, temp, "Error", JOptionPane.ERROR_MESSAGE);
			tbTabs.setSelectedIndex(1);
			return;
		}
		tbTabs.setSelectedIndex(2);
	}
	else if(e.getSource() == btNextToWho) {
		if((temp = validateWhereTab()) != null) {
			JOptionPane.showMessageDialog(this, temp, "Error", JOptionPane.ERROR_MESSAGE);
			tbTabs.setSelectedIndex(2);
			return;
		}
		tbTabs.setSelectedIndex(3);
	}
	else if(e.getSource() == btBackToWhere) {
		tbTabs.setSelectedIndex(2);
	}
	else if(e.getSource() == btBackToHow) {
		tbTabs.setSelectedIndex(1);
	}
	else if(e.getSource() == btBackToWhat) {
		tbTabs.setSelectedIndex(0);
	}
	else if(e.getSource() == btExport) {
		if((temp = validateWhatTab()) != null) {
			JOptionPane.showMessageDialog(this, temp, "Error", JOptionPane.ERROR_MESSAGE);
			tbTabs.setSelectedIndex(0);
			return;
		}
		if((temp = validateHowTab()) != null) {
			JOptionPane.showMessageDialog(this, temp, "Error", JOptionPane.ERROR_MESSAGE);
			tbTabs.setSelectedIndex(1);
			return;
		}
		if((temp = validateWhereTab()) != null) {
			JOptionPane.showMessageDialog(this, temp, "Error", JOptionPane.ERROR_MESSAGE);
			tbTabs.setSelectedIndex(2);
			return;
		}
		if((temp = validateWhoTab()) != null) {
			JOptionPane.showMessageDialog(this, temp, "Error", JOptionPane.ERROR_MESSAGE);
			tbTabs.setSelectedIndex(3);
			return;
		}
		inputObtained = true;
		// hide();
	}
	else if(e.getSource() == btAdd) {
		Object[] sv = lsOrigFields.getSelectedValues();
		for(int i=0;i<sv.length;i++) {
			if(!lmChosenFields.contains((String) sv[i])) lmChosenFields.addElement((String) sv[i]);
			lmOrigFields.removeElement((String) sv[i]);
		}
	}
	else if(e.getSource() == btRemove) {
		Object[] sv = lsChosenFields.getSelectedValues();
		for(int i=0;i<sv.length;i++) {
			if(!lmOrigFields.contains((String) sv[i])) lmOrigFields.addElement((String) sv[i]);
			lmChosenFields.removeElement((String) sv[i]);
		}
	}
	else if(e.getSource() == btBrowse) {
		if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			tfFile.setText(jfc.getSelectedFile().getAbsolutePath());
		}

	}
}

public String validateWhatTab() {

	String what = "all";
	if(rbAll.isSelected()) what = "all";
	else if(rbSearch.isSelected()) what = "search";
	else if(rbCustom.isSelected()) what = "custom";

	String search = "";
	String field = "name";
	String rule = "contains";
	if(what.equals("search")) {
		search = tfSearch.getText();
	}
	else if(what.equals("custom")) {
		search = tfCustomSearch.getText();
		rule = (String) cmbRule.getSelectedItem();
		field = (String) cmbFields.getSelectedItem();
	}
	exData.exportType = what;
	exData.searchRule = rule;
	exData.searchString = search;
	exData.searchField = field;

	// System.out.println("Data What: " + what + "-" + search + "-" + field + "-" + rule);
	return null;
}

public String validateHowTab() {

	int count = 1;
	String sep = ",";
	if(rbTab.isSelected()) sep = "\t";
	else if(rbSpace.isSelected()) sep = " ";
	else if(rbOther.isSelected()) sep = tfOther.getText();

	try {
		count = Integer.parseInt(tfNumber.getText());
	} catch(Exception x) {
		// System.out.println("Invalid number " + tfNumber.getText());
		return "Invalid number " + tfNumber.getText();
	}
	if(count < 1) return "Invalid number " + count;
	String s = "";
	for(int i=0;i<count;i++) s += sep;

	ListModel lm = lsChosenFields.getModel();
	String[] chosen = new String[lm.getSize()];
	for(int i=0;i<chosen.length;i++) {
		chosen[i] = (String) lm.getElementAt(i);
	}

	// System.out.println("Sep Char = " + sep);
	// System.out.println("Number = " + count);
	// System.out.println("Separator = " + s);
	exData.separator = s;
	exData.separatorChar = sep;
	exData.separatorCount = String.valueOf(count);
	exData.exportFields = chosen;
 	/*for(int i=0;i<chosen.length;i++) {
		System.out.println(chosen[i]);
	}*/
	return null;
}

public String validateWhereTab() {

	String fileName = null;
	if(tfFile.getText().trim().equals("")) return "Invalid File Name";
	fileName = tfFile.getText().trim();
	// System.out.println("File = " + fileName);
	exData.fileLocation = fileName;
	return null;
}

public String validateWhoTab() {

	String user = null, pass = null;
	if(tfUser.getText().trim().equals("")) return "Invalid User Name";
	if(String.valueOf(tfPass.getPassword()).trim().equals("")) return "Invalid Password";
	user = tfUser.getText().trim();
	pass = String.valueOf(tfPass.getPassword());
	// System.out.println("Who Data: " + user + "-" + pass);
	exData.user = user;
	exData.password = pass;
	return null;
}

public GExInfo getExportData() {
	if(inputObtained) return exData;
	return null;
}

public void initValues(GExInfo data) {

	inputObtained = false;
	lmChosenFields.clear();
	lmOrigFields.clear();
	lmOrigFields.addElement("Name");
	lmOrigFields.addElement("ID (used by gmail)");
	lmOrigFields.addElement("Email");
	lmOrigFields.addElement("Notes");

	if(data == null) return;
	tfUser.setText(data.user);
	tfPass.setText(data.password);
	tfFile.setText(data.fileLocation);
	tfNumber.setText(data.separatorCount);
	if(data.separatorChar.equals("\t")) rbTab.setSelected(true);
	else if(data.separatorChar.equals(" ")) rbSpace.setSelected(true);
	else { rbOther.setSelected(true); tfOther.setText(data.separatorChar); }
	if(data.exportFields != null) {
		for(int i=0;i<data.exportFields.length;i++) {
			lmChosenFields.addElement(data.exportFields[i]);
		}
	}
	if(data.exportType.equalsIgnoreCase("all")) rbAll.setSelected(true);
	else if(data.exportType.equalsIgnoreCase("search")) { rbSearch.setSelected(true); tfSearch.setText(data.searchString); }
	else {
		rbCustom.setSelected(true);
		tfCustomSearch.setText(data.searchString);
		cmbRule.setSelectedItem(data.searchRule);
		cmbFields.setSelectedItem(data.searchField);
	}
}
}
